package com.keyancompany.zhongbaopingtai.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;

import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.MessageProAdapter;
import com.keyancompany.zhongbaopingtai.model.MessageModel;
import com.keyancompany.zhongbaopingtai.weight.IListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindString;

public class ProjectMessageActivity extends BaseMessageActivity {

    @BindString(R.string.project_message_text)
    public String title;
    @Bind(R.id.toolbar)
    public Toolbar toolbar;

    @Bind(R.id.listView)
    IListView listView;
    @Bind(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;

    private MessageProAdapter messageAdapter;

    private MessageModel messageModel;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_project_message;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        initSwipeLayout(swipeLayout);
        messageModel = (MessageModel) getIntent().getSerializableExtra(Constants.ITEM);

        messageAdapter = new MessageProAdapter(this);
        listView.setAdapter(messageAdapter);

        swipeLayout.setOnRefreshListener(this);
        swipeLayout.post(new Thread(new Runnable() {

            @Override
            public void run() {
                swipeLayout.setRefreshing(true);
                onRefresh();
            }
        }));
    }

    @Override
    void updateList(List<MessageModel> list) {
        if(total == 1){
            messageAdapter.clear();
        }
        if(list.isEmpty()){
            return ;
        }
        if (messageAdapter == null) return;
        listView.loadMoreFinish();
        if (total == currentPage) {
            listView.removeFooterView();
        }

        messageAdapter.addAllAfter(list);
    }

    @Override
    void refreshingFinish() {
        swipeLayout.setRefreshing(false);
    }


    @Override
    public void onRefresh() {
        requestTask(messageModel.getType(),messageModel.getFromUser());
    }
}
